<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数字限制范围示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7f6;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            text-align: center;
        }

        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 80%;
            max-width: 500px;
        }

        h1 {
            color: #4CAF50;
            margin-bottom: 20px;
        }

        .input-group {
            margin: 20px 0;
        }

        .input-group label {
            font-size: 18px;
            margin-right: 10px;
        }

        .input-group input {
            padding: 8px;
            font-size: 16px;
            text-align: center;
            margin-right: 10px;
            display: block;
            margin: 10px auto;
            outline: none;
            border-radius: 4px;
            border: 1px solid #dedede;
        }

        .input-group input:focus {
            border-color: #4CAF50;
        }

        .btn {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .btn:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 30px;
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }

        .result span {
            color: #4CAF50;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>数字限制范围示例</h1>
        <p>输入一个数字，并设置最小值和最大值，点击按钮来限制该数字的范围。</p>

        <div class="input-group">
            <label for="numInput">输入数字:</label>
            <input type="number" id="numInput" placeholder="输入数字" />
        </div>

        <div class="input-group">
            <label for="minInput">最小值:</label>
            <input type="number" id="minInput" placeholder="最小值" />
            <label for="maxInput">最大值:</label>
            <input type="number" id="maxInput" placeholder="最大值" />
        </div>

        <button class="btn" onclick="clampAndShowResult()">计算</button>

        <div class="result" id="result"></div>
    </div>

    <script>
        // clampNumber 函数实现：限制数字在最小值和最大值之间
        const clampNumber = (num, a, b) =>
            Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));

        // 计算并显示结果
        function clampAndShowResult() {
            const num = parseFloat(document.getElementById("numInput").value);
            const min = parseFloat(document.getElementById("minInput").value);
            const max = parseFloat(document.getElementById("maxInput").value);

            // 确保输入有效
            if (isNaN(num) || isNaN(min) || isNaN(max)) {
                alert("请输入有效的数字！");
                return;
            }

            // 使用 clampNumber 函数限制数字
            const result = clampNumber(num, min, max);

            // 显示结果
            const resultDiv = document.getElementById("result");
            resultDiv.innerHTML = `结果：<span>${result}</span>`;
        }
    </script>
</body>

</html>